<?php



/**
 * array_column  php5.5新增函数
 * 增强array_column函数，适应php低版本
 *
 *
 * @param $input
 * @param $columnKey
 * @param null $indexKey
 * @return array
 */
function i_array_column($input, $columnKey, $indexKey=null){
    if(!function_exists('array_column')){
        $columnKeyIsNumber  = (is_numeric($columnKey))?true:false;
        $indexKeyIsNull            = (is_null($indexKey))?true :false;
        $indexKeyIsNumber     = (is_numeric($indexKey))?true:false;
        $result                         = array();
        foreach((array)$input as $key=>$row){
            if($columnKeyIsNumber){
                $tmp= array_slice($row, $columnKey, 1);
                $tmp= (is_array($tmp) && !empty($tmp))?current($tmp):null;
            }else{
                $tmp= isset($row[$columnKey])?$row[$columnKey]:null;
            }
            if(!$indexKeyIsNull){
                if($indexKeyIsNumber){
                    $key = array_slice($row, $indexKey, 1);
                    $key = (is_array($key) && !empty($key))?current($key):null;
                    $key = is_null($key)?0:$key;
                }else{
                    $key = isset($row[$indexKey])?$row[$indexKey]:0;
                }
            }
            $result[$key] = $tmp;
        }
        return $result;
    }else{
        return array_column($input, $columnKey, $indexKey);
    }
}



/**
 * 计算当前为学期第几周，星期几，正在上第几节课
 *
 *
 * @param datetime $stime    学期开始时间
 * @param datetime $etime    学期结束时间
 * @return bool|array        返回false表示当前不在上课时间
 */
function getTimeInfo($stime,$etime,$onlyWeek=false) {


    $stime = strtotime($stime);
    $etime = strtotime($etime);
    $now = time();
    if ($now < $stime || $now > $etime)
        return false;

    $diff_time = $now - $stime;
    $week = intval(ceil($diff_time/604800));
    if ($onlyWeek) return $week;
    $xq = intval(date('w',time()));
    $xq === 0 && $xq = 7;

    $schedule = M ('Schedule')->select();

    $_t = time();
    foreach ($schedule as $k=>$v) {
        $st = strtotime($v['stime']);
        $et = strtotime($v['etime']);
        if ($_t < $et && $_t > $st) {
            $jc = $v['section'];
        }
    }

    if (empty($jc)) return false;
    return array(
        'week' => $week,
        'xq' => $xq,
        'jc' => $jc,
    );

}

